//
//  CookDetailsViewController.m
//  Delicious2
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "CookDetailsViewController.h"
#import "HealthLightViewController.h"

@interface CookDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) CookDetaileSecondModel *secondModel;
@property (nonatomic, strong) CookDetaileModel *detaileModel;

@property (nonatomic, strong) MBProgressHUD *HUD;

@end

@implementation CookDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self creatTableView];
    [self getData];
    self.navigationItem.rightBarButtonItem = nil;
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
}


#pragma maek - tableView头视图
- (void)creatTabaleViewHeaderView {
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 100)];
    headerView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *imageHeader = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];
    [imageHeader sd_setImageWithURL:[NSURL URLWithString:self.detaileModel.thumb_image_url] placeholderImage:[UIImage imageNamed:@"ImagePlace"]];
    imageHeader.layer.masksToBounds = YES;
    imageHeader.layer.cornerRadius = 15;
    
    UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, WIDTH - imageHeader.frame.size.width - 20, 40)];
    labelTitle.text = self.detaileModel.name;
    labelTitle.font = [UIFont boldSystemFontOfSize:20];
    
    UILabel *labelDown = [[UILabel alloc] initWithFrame:CGRectMake(100, labelTitle.frame.origin.y + labelTitle.frame.size.height, WIDTH - imageHeader.frame.size.width - 20, 40)];
    NSString *strDown = [NSString stringWithFormat:@"%@ 大卡/每100克(可食部分)", self.detaileModel.calory];
    //关键字高亮
    NSString *str = strDown;
    NSRange range = [str rangeOfString:self.detaileModel.calory];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:str];
    [string addAttribute:NSForegroundColorAttributeName value:kColor range:NSMakeRange(range.location, range.length)];
    labelDown.attributedText = string;
    

    [headerView addSubview:imageHeader];
    [headerView addSubview:labelTitle];
    [headerView addSubview:labelDown];
    [headerView nightMode];
    self.tableView.tableHeaderView = headerView;
}

#pragma mark - creatTableView
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
    
    [self.view addSubview:self.tableView];
}

#pragma mark - tableViewDelegate 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return self.secondKeyDataSource.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"UITableViewCellIdentifier"];
    //数组保护
    if (self.secondKeyDataSource.count != 0) {
        if (indexPath.section == 0) {
            //是否有评价
            if (self.detaileModel.appraise) {
                cell.textLabel.text = self.detaileModel.appraise;
            } else {
                cell.textLabel.text = @"暂无评价";
            }
            //减肥红绿灯
            if (self.detaileModel.health_light.intValue == 1) {
                cell.imageView.image = [UIImage imageNamed:@"ImageTuijian"];
                
            } else if (self.detaileModel.health_light.intValue == 2) {
                cell.imageView.image = [UIImage imageNamed:@"ImageShiliang"];
            } else {
                cell.imageView.image = [UIImage imageNamed:@"ImageShaochi"];
            }
            
        } else {
            NSString *str = [NSString stringWithFormat:@"%@     ", self.secondValueDataSource[indexPath.row]];
            cell.textLabel.text = self.secondKeyDataSource[indexPath.row];
            cell.detailTextLabel.text = str;
        }

    }
    [cell nightMode];
    cell.textLabel.numberOfLines = 5;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

//分区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 44)];
//        [viewHeader nightMode];
        UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width / 2 - 10, 44)];
        labelTitle.text = @"营养元素";
//        [labelTitle nightMode];
        UILabel *labelSubTitle = [[UILabel alloc] initWithFrame:CGRectMake(labelTitle.frame.size.width, 0, labelTitle.frame.size.width, labelTitle.frame.size.height)];
        labelSubTitle.text = @"每100克";
        labelSubTitle.textAlignment = NSTextAlignmentRight;
//        [labelSubTitle nightMode];
        [viewHeader addSubview:labelTitle];
        [viewHeader addSubview:labelSubTitle];
        return viewHeader;
    } else {
        UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width / 2 - 10, 44)];
        labelTitle.text = @"食·评价";
        return labelTitle;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 150;
    } else {
        return 44;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HealthLightViewController *healthVC = [[HealthLightViewController alloc] init];
        
        [self.navigationController pushViewController:healthVC animated:YES];
    }
}

#pragma mark - 获取数据
- (void)getData {
    
    NSString *strUrl = [[kDetaileGETURL stringByAppendingString:@"/"] stringByAppendingString:self.strCode];
    [FSDNetWorkTool getWithURL:strUrl withParameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        
        self.detaileModel = [[CookDetaileModel alloc] initWithDictionary:result];
        self.navigationItem.title = self.detaileModel.name;
        
        self.secondModel = [[CookDetaileSecondModel alloc] initWithDictionary:result[@"ingredient"]];
        
        [self setSecondDataSource];
        [self creatTabaleViewHeaderView];
        [self.tableView reloadData];
        self.HUD.hidden = YES;
        } fail:^(NSError *error) {
        NSLog(@"错误");
        self.HUD.labelText = @"加载失败";
        [self.HUD hide:YES afterDelay:3];
    }];
}

//建立营养元素数组
- (void)setSecondDataSource {
 
    self.secondKeyDataSource = [[NSArray alloc] initWithObjects:@"热量", @"蛋白质", @"脂肪", @"碳水化合物", @"膳食纤维", @"维生素A", @"维生素C", @"维生素E", @"胡萝卜素", @"维生素B1(...", @"维生素B2(...", @"烟酸", @"胆固醇", @"镁", @"钙", @"铁", @"锌", @"铜", @"锰", @"钾", @"磷", @"钠", @"硒", nil];
    
    NSString *str1 = [NSString stringWithFormat:@"%@大卡", self.secondModel.calory];
    NSString *str2 = [NSString stringWithFormat:@"%@克", self.secondModel.protein];
    NSString *str3 = [NSString stringWithFormat:@"%@克", self.secondModel.fat];
    NSString *str4 = [NSString stringWithFormat:@"%@克", self.secondModel.carbohydrate];
    NSString *str5 = [NSString stringWithFormat:@"%@克", self.secondModel.fiber_dietary];
    NSString *str6 = [NSString stringWithFormat:@"%@微克", self.secondModel.vitamin_a];
    NSString *str7 = [NSString stringWithFormat:@"%@毫克", self.secondModel.vitamin_c];
    NSString *str8 = [NSString stringWithFormat:@"%@毫克", self.secondModel.vitamin_e];
    NSString *str9 = [NSString stringWithFormat:@"%@微克", self.secondModel.carotene];
    NSString *str10 = [NSString stringWithFormat:@"%@毫克", self.secondModel.thiamine];
    NSString *str11 = [NSString stringWithFormat:@"%@毫克", self.secondModel.lactoflavin];
    NSString *str12 = [NSString stringWithFormat:@"%@毫克", self.secondModel.niacin];
    NSString *str13 = [NSString stringWithFormat:@"%@毫克", self.secondModel.cholesterol];
    NSString *str14 = [NSString stringWithFormat:@"%@毫克", self.secondModel.magnesium];
    NSString *str15 = [NSString stringWithFormat:@"%@毫克", self.secondModel.calcium];
    NSString *str16 = [NSString stringWithFormat:@"%@微克", self.secondModel.iron];
    NSString *str17 = [NSString stringWithFormat:@"%@毫克", self.secondModel.zinc];
    NSString *str18 = [NSString stringWithFormat:@"%@毫克", self.secondModel.copper];
    NSString *str19 = [NSString stringWithFormat:@"%@微克", self.secondModel.manganese];
    NSString *str20 = [NSString stringWithFormat:@"%@毫克", self.secondModel.kalium];
    NSString *str21 = [NSString stringWithFormat:@"%@毫克", self.secondModel.phosphor];
    NSString *str22 = [NSString stringWithFormat:@"%@毫克", self.secondModel.natrium];
    NSString *str23 = [NSString stringWithFormat:@"%@微克", self.secondModel.selenium];
    
    self.secondValueDataSource = [[NSArray alloc] initWithObjects:str1,str2, str3, str4, str5, str6,str7, str8, str9, str10, str11,str12, str13, str14, str15, str16,str17, str18, str19, str20, str21,str22,str23, nil];
 
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.view nightMode];
    [self.tableView.tableHeaderView nightMode];
    [self.tableView nightMode];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
